package lax.task.com;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.*;

class MyHandler implements HttpHandler {

    public void handle(HttpExchange exchange) throws IOException {
        String requestMethod = exchange.getRequestMethod();
        if (requestMethod.equalsIgnoreCase("GET")) {
            Headers responseHeaders = exchange.getResponseHeaders();
            Map<String, String> params = this.queryToMap(exchange.getRequestURI().getQuery());
            int time = Integer.parseInt(params.get("time"));
            if (time > 1500000) {
                String mode_str = params.get("mode");
                if (mode_str.equals("")) {
                    mode_str = "add";
                }
                String name_str = params.get("name");
                String url_str = params.get("url");
                String url = URLDecoder.decode(url_str, "UTF-8");
                System.out.println("AddTaskModel:" + mode_str);
                int index = -1;
                if (AppData.TaskList.size() > 0) {
                    for (int i = 0; i < AppData.TaskList.size(); i++) {
                        if (mode_str.equals(AppData.TaskList.get(i).getName())) {
                            index = i;
                            break;
                        }
                    }
                }
                if (mode_str.equals("add")) {
                    if (index >= 0) {
                        AppData.TaskList.get(index).setTime(time);
                        AppData.TaskList.get(index).setUrl(url);
                    } else {
                        AppData.TaskList.add(new TaskData(time, url, name_str));
                    }
                    AppData.TaskList = Tool.sortMapByValue(AppData.TaskList);
                    System.out.println("AddTask:" + name_str);
                } else if (mode_str.equals("del")) {
                    if (index > 0) {
                        AppData.TaskList.remove(index);
                        AppData.TaskList = Tool.sortMapByValue(AppData.TaskList);
                        System.out.println("DelTask:" + name_str);
                    }
                }
            }
            responseHeaders.set("Content-Type", "text/plain");
            exchange.sendResponseHeaders(200, 0);
            OutputStream responseBody = exchange.getResponseBody();
//            Headers requestHeaders = exchange.getRequestHeaders();
//            Set<String> keySet = requestHeaders.keySet();
//            Iterator<String> iter = keySet.iterator();
//            while (iter.hasNext()) {
//                String key = iter.next();
//                List values = requestHeaders.get(key);
//                String s = key + " = " + values.toString() + "\n";
//                responseBody.write(s.getBytes());
//            }
            responseBody.write("ok".getBytes());
            responseBody.close();
        }
    }

    public Map<String, String> queryToMap(String query) {
        Map<String, String> result = new HashMap<String, String>();
        for (String param : query.split("&")) {
            String pair[] = param.split("=");
            if (pair.length > 1) {
                result.put(pair[0], pair[1]);
            } else {
                result.put(pair[0], "");
            }
        }
        return result;
    }
}